﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using System.Collections.Generic;
using System.Threading.Tasks;
using TabbedTemplate.Converters;
using TabbedTemplate.Models;
using TabbedTemplate.Services;
using Xamarin.Forms.Extended;

namespace TabbedTemplate.ViewModels
{
    public class StoreCollectionsPageViewModel : ViewModelBase
    {
        public string Status
        {
            get => _status;
            set => Set(nameof(Status), ref _status, value);
        }

        private string _status;

        public const string Loading = "正在载入";

        public const string NoResult = "暂无任何收藏店铺";

        public const string NoMoreResult = "没有更多收藏";

        public InfiniteScrollCollection<StoreFavorite> StoreFavoriteCollection
        {
            get;
            set;
        }

        public bool CanLoadMore;

        private readonly IFavoriteStorage _favoriteStorage;

        private readonly IStoreStorage _storeStorage;

        private readonly IPunchNavigationService _punchNavigationService;

        public StoreCollectionsPageViewModel(IFavoriteStorage favoriteStorage,
            IStoreStorage storeStorage,
            IPunchNavigationService punchNavigationService)
        {
            _favoriteStorage = favoriteStorage;
            _storeStorage = storeStorage;
            _punchNavigationService = punchNavigationService;
            StoreFavoriteCollection =
                new InfiniteScrollCollection<StoreFavorite>();
        }

        /// <summary>
        /// 页面显示命令。
        /// </summary>
        public RelayCommand PageAppearingCommand =>
            _pageAppearingCommand ??= new RelayCommand(async () =>
                await PageAppearingCommandFunction());

        /// <summary>
        /// 页面显示命令。
        /// </summary>
        private RelayCommand _pageAppearingCommand;

        public async Task PageAppearingCommandFunction()
        {
            StoreFavoriteCollection.Clear();
            StoreFavoriteCollection.OnCanLoadMore = () => CanLoadMore;
            StoreFavoriteCollection.OnLoadMore = async () =>
            {
                Status = Loading;
                List<StoreFavorite> storeFavorites = new List<StoreFavorite>();
                var favoriteList =
                    await _favoriteStorage.GetFavoritesAsync(
                        StoreFavoriteCollection.Count, StoreService.PageSize);

                if (!_storeStorage.UserCacheInitialized())
                {
                    await _storeStorage.UserCacheInitializeAsync();
                }

                foreach (Favorite favorite in favoriteList)
                {
                    var storeFavorite = new StoreFavorite(
                        await _storeStorage.GetStoreByStoreUidAsync(
                            favorite.StoreUid), favorite,
                        ConvertTimeStamp.ConvertTimeStampToDate(favorite
                            .Timestamp));
                    storeFavorites.Add(storeFavorite);
                }

                if (storeFavorites.Count < StoreService.PageSize)
                {
                    CanLoadMore = false;
                    Status = NoMoreResult;
                }
                else
                {
                    Status = "";
                }

                if (StoreFavoriteCollection.Count == 0 &&
                    storeFavorites.Count == 0)
                {
                    Status = NoResult;
                }

                return storeFavorites;
            };
            CanLoadMore = true;
            await StoreFavoriteCollection.LoadMoreAsync();
        }

        private RelayCommand<StoreFavorite> _storeTappedCommand;

        public RelayCommand<StoreFavorite> StoreTappedCommand =>
            _storeTappedCommand ??= new RelayCommand<StoreFavorite>(
                async storeFavorite =>
                    await StoreTappedCommandFunction(storeFavorite));

        public async Task StoreTappedCommandFunction(
            StoreFavorite storeFavorite)
        {
            var store = storeFavorite.Store;
            await _punchNavigationService.UserPageNavigationToAsync(
                ContentNavigationConstant.StorePunchesPage, store);
        }
    }
}
